package com.amsp.oa.oademo.service.impl;

import com.amsp.oa.oademo.entity.VehicleApplication;
import com.amsp.oa.oademo.mapper.VehicleApplicationMapper;
import com.amsp.oa.oademo.service.VehicleService;
import com.amsp.oa.oademo.model.request.VehicleDispatchRequest;
import com.amsp.oa.oademo.model.request.VehicleRecordRequest;
import com.amsp.oa.oademo.model.request.VehicleReturnRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.List;
import org.springframework.util.StringUtils;

@Service
public class VehicleServiceImpl implements VehicleService {

    @Autowired
    private VehicleApplicationMapper applicationMapper;

    @Override
    public String createApplication(VehicleApplication application) {
        // 基本参数验证
        if (StringUtils.isEmpty(application.getDepartment()) 
            || StringUtils.isEmpty(application.getApplicant())
            || StringUtils.isEmpty(application.getPhone())
            || application.getUseStartTime() == null
            || application.getUseEndTime() == null) {
            throw new RuntimeException("请填写完整的申请信息");
        }

        // 设置申请编号和初始状态
        application.setApplyNo(generateApplyNo());
        application.setStatus("pending");
        application.setCreateTime(LocalDateTime.now());
        application.setUpdateTime(LocalDateTime.now());

        // 保存申请
        try {
            applicationMapper.insert(application);
            return application.getApplyNo();
        } catch (Exception e) {
            throw new RuntimeException("保存申请失败：" + e.getMessage());
        }
    }

    @Override
    public void approveApplication(Long id, String result, String remark) {
        VehicleApplication application = applicationMapper.selectById(id);
        if (application == null) {
            throw new RuntimeException("申请不存在");
        }
        application.setStatus(result);
        application.setRemark(remark);
        application.setUpdateTime(LocalDateTime.now());
        applicationMapper.updateById(application);
    }

    @Override
    public List<VehicleApplication> getApplicationList(String status) {
        return applicationMapper.selectByStatus(status);
    }

    @Override
    public VehicleApplication getApplicationById(Long id) {
        return applicationMapper.selectById(id);
    }

    @Override
    public List<VehicleApplication> getPendingApprovals() {
        return applicationMapper.selectByStatus("pending");
    }

    @Override
    public List<VehicleApplication> getApprovedApplications() {
        return applicationMapper.selectByStatus("approved");
    }

    @Override
    public void dispatchVehicle(Long id, VehicleDispatchRequest request) {
        VehicleApplication application = applicationMapper.selectById(id);
        if (application == null) {
            throw new RuntimeException("申请不存在");
        }
        if (!"approved".equals(application.getStatus())) {
            throw new RuntimeException("申请未审批通过");
        }
        
        // 更新派遣信息
        application.setVehicleNo(request.getVehicleNo());
        application.setVehicleType(request.getVehicleType());
        application.setDriver(request.getDriver());
        application.setDriverPhone(request.getDriverPhone());
        application.setStartMileage(request.getStartMileage());
        application.setFuelLevel(request.getFuelLevel());
        application.setDispatchTime(LocalDateTime.now());
        application.setStatus("dispatched");
        application.setUpdateTime(LocalDateTime.now());
        
        applicationMapper.updateDispatchInfo(application);
    }
    
    @Override
    public List<VehicleApplication> getPendingDispatchList() {
        return applicationMapper.selectByStatus("approved");
    }

    @Override
    public List<VehicleApplication> getDispatchedList() {
        return applicationMapper.selectByStatus("dispatched");
    }

    @Override
    public void recordVehicleUsage(Long id, VehicleRecordRequest request) {
        VehicleApplication application = applicationMapper.selectById(id);
        if (application == null) {
            throw new RuntimeException("申请不存在");
        }
        if (!"dispatched".equals(application.getStatus())) {
            throw new RuntimeException("车辆未派遣");
        }

        // 更新用车记录
        application.setEndMileage(request.getEndMileage());
        application.setEndFuel(request.getEndFuel());
        application.setCondition(request.getCondition());
        application.setAbnormalDesc(request.getAbnormalDesc());
        application.setReturnTime(LocalDateTime.now());
        application.setStatus("returned");
        application.setUpdateTime(LocalDateTime.now());

        applicationMapper.updateReturnInfo(application);
    }

    @Override
    public List<VehicleApplication> getInUseList() {
        return applicationMapper.selectByStatus("dispatched");
    }

    @Override
    public List<VehicleApplication> getCompletedList() {
        return applicationMapper.selectByStatus("completed");
    }

    @Override
    public void confirmReturn(Long id, VehicleReturnRequest request) {
        VehicleApplication application = applicationMapper.selectById(id);
        if (application == null) {
            throw new RuntimeException("申请不存在");
        }
        if (!"returned".equals(application.getStatus())) {
            throw new RuntimeException("车辆未归还");
        }

        // 更新确认信息
        application.setStatus("completed");
        application.setRemark(request.getRemark());
        application.setUpdateTime(LocalDateTime.now());

        applicationMapper.updateById(application);
    }

    @Override
    public List<VehicleApplication> getPendingReturnList() {
        return applicationMapper.selectByStatus("returned");
    }

    @Override
    public List<VehicleApplication> getReturnedList() {
        return applicationMapper.selectByStatus("completed");
    }

    private String generateApplyNo() {
        return "YC" + LocalDateTime.now().format(java.time.format.DateTimeFormatter.BASIC_ISO_DATE)
                + String.format("%03d", (int)(Math.random() * 1000));
    }
} 